// mypage/myAccount/myAccount.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");


Page({
  /*页面的初始数据*/
  data: {
    uid: '',
    info: '',
    mech_cach_info: '',
    bala_money: '0.00',
    cash_money: '0.00',
    mech_list: [],
    mech_index: 0,
    mech_id: '',
    businessName: '',
    fun_list: [{img:"/images/icon73.png",title:'账单'},{img:"/images/icon74.png",title:'提现'}],
    show_money: true,
    // card_list:[],
    // dian: ["","","",""],
    // show_card: false,
    // real_name: true,
    show_mask: true,
    fun_page: ["../accBill/accBill","../accCash/accCash","../accDeposit/accDeposit","../accCard/accCard"],
    token: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this,uid=options.id;
    that.setData({uid:uid,token:wx.getStorageSync('token')})
    that.get_personal_dom(uid)
  },
  // 隐藏money
  hide_money:function(){
    var that = this,bala_money=that.data.bala_money,info=that.data.mech_cach_info,show_money=that.data.show_money,str='';
    if(show_money){
      for(var i=0;i<bala_money.length;i++){
        str += '*'
      }
    }else{
      if(info){
        str=info.balance>=1000?util.toThousands(util.toDecimal(info.balance)):util.toDecimal(info.balance)
      }else{
        str='0.00'
      }
    }
    that.setData({bala_money: str,show_money:!show_money});
  },
  // 选择机构
  check_mech:function(){
    var that=this,index=that.data.mech_index;
    wx.navigateTo({
      url: '../checkMech/checkMech?index='+index,
    })
  }, 
  // 设置
  // nav_send:function(){
  //   var that=this,real_name=that.data.real_name;
  //   if(real_name){
  //     wx.navigateTo({
  //       url: '../accSetting/accSetting',
  //     })
  //   }else{
  //     that.setData({show_mask:!that.data.show_mask})
  //   }
  // },
  // 跳转
  nav_detail:function(e){
    var that=this,real_name=that.data.real_name,fun_page=that.data.fun_page,info=that.data.info,mech_id=that.data.mech_id;
    var index=e.currentTarget.dataset.index;
    if(mech_id){
      that.get_business_type(mech_id,index)
    }else{
      util.page_show_toast('您还未有提现机构',2000)
    }
    // if(real_name){
    //   wx.navigateTo({
    //     url: fun_page[index],
    //   })
    // }else{
    //   that.setData({show_mask:!that.data.show_mask})
    // }
  },
  // close_mask:function(){
  //   this.setData({show_mask:!this.data.show_mask})
  // },
  // 跳转实名制
  // nav_real:function(){
  //   var that = this;
  //   wx.navigateTo({
  //     url: '../accRealname/accRealname',
  //   })
  //   that.setData({show_mask:!that.data.show_mask})
  // },
  // nav_bank:function(){
  //   var that=this,real_name=that.data.real_name;
  //   if(real_name){
  //     wx.navigateTo({
  //       url: '../accCard/accCard',
  //     })
  //   }else{
  //     that.setData({show_mask:!that.data.show_mask})
  //   }
  // },
  onShow:function(){
    var that=this,mech_id=that.data.mech_id;
    if(mech_id){
      that.get_mech_cach(mech_id)
    }
  },
  // 获取用户详情
  get_personal_dom:function(id){
    wx.showLoading({title:"加载中"})
    var that=this;
    my.get_personal_dom({"id":id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        info.identity=2;
        that.setData({info:info})
        if(info.identity==2){
          that.get_org_audi_list();
        }
      }
    })
  },
  // 获取分校列表
  get_org_audi_list:function(){
    var that=this,token=that.data.token,phone=that.data.info.phone;
    var params={
      "phone": phone
    }
    my.get_org_audi_list(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        // console.log(list)
        if(list.length>0){
          that.setData({mech_list: list,businessName:list[0].businessName,mech_id:list[0].businessId})
          that.get_mech_cach(list[0].businessId)
        }
      }
    })
  },
  // 根据机构编号查询机构账户余额
  get_mech_cach:function(id){
    var that=this;
    my.get_mech_cach({"businessId":id}).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        var bala_money=info.balance>=1000?util.toThousands(util.toDecimal(info.balance)):util.toDecimal(info.balance),
            cash_money=info.withdrawableBalance>=1000?util.toThousands(util.toDecimal(info.withdrawableBalance)):util.toDecimal(info.withdrawableBalance);
        //
        that.setData({bala_money:bala_money,cash_money:cash_money,mech_cach_info:info})
      }
    })
  },
  // 判断机构是否绑定提现微信
  get_business_type:function(id,index){
    var that=this,fun_page=that.data.fun_page,token=that.data.token;
    var params={
      "businessId": id,
	    "type": 1
    }
    my.get_business_type(params,token).then(res=>{
      if(res.data.code==200){
        if(res.data.data){
          wx.navigateTo({
            url: fun_page[index]+'?index='+that.data.mech_index+'&money='+that.data.cash_money+'&name='+that.data.businessName+'&id='+id,
          })
        }else{
          util.page_show_toast(res.data.message,3000)
        }
      }
    })
  },
})